﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayerHelper;

/// <summary>
/// Summary description for ListControlBase
/// </summary>
public abstract  class ListControlBase<T>: UserControl
{
    PagedDataSource pgitems;
    DropDownList ddlPages;
    Repeater rptItems;

    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    if (!Page.IsPostBack)
    //        LoadData();
    //}
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ddlPages = (DropDownList)this.FindControl("ddlPages");
        rptItems = (Repeater)this.FindControl("rptItems");
        pgitems = new PagedDataSource();


        // rptPages.ItemCommand += new RepeaterCommandEventHandler(rptPages_ItemCommand);
        ddlPages.SelectedIndexChanged += new EventHandler(ddlPages_SelectedIndexChanged);
        ddlPages.AutoPostBack = true;
    }

    void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        PageNumber = int.Parse(ddlPages.SelectedItem.Value);
        LoadData();

    }

    public void LoadData()
    {

        pgitems.DataSource = GetListObjects;
        pgitems.AllowPaging = true;
        pgitems.PageSize = 25;
        pgitems.CurrentPageIndex = PageNumber;
        if (pgitems.PageCount > 1)
        {

            SetPageDropDrowList();

        }

        rptItems.DataSource = pgitems;
        rptItems.DataBind();
    }

    void SetPageDropDrowList()
    {

        List<ListItem> list = new List<ListItem>();
        for (int i = 0; i < pgitems.PageCount; i++)
        {

            ListItem listItem = new ListItem(i + "", "" + i);

            listItem.Selected = true;
            list.Add(listItem);
            ddlPages.DataSource = list;
            

        }

        ddlPages.SelectedValue = "" + PageNumber;
        ddlPages.DataBind();

    }
    public abstract PersistentList<T> GetListObjects { get; }

    void rptPages_ItemCommand(object source,
                              RepeaterCommandEventArgs e)
    {
        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        LoadData();
    }

    public string DoEdit(string id)
    {
        string page = Request["page"];
        string action = "Edit";
        return DoAction.ActionWithID(page, action, "id=" + id);
    }
    public string DoView(string id)
    {
        string page = Request["page"];
        string action = "View";
        return DoAction.ActionWithID(page, action, "id=" + id);
    }
    public string DoView(string page,string id)
    {
        //string page = Request["page"];
        string action = "List";
        return DoAction.ActionWithID(page, action, "id=" + id);
    }
    public string DoView(string page,string action, string id)
    {
        //string page = Request["page"];
       // string action = action;
        return DoAction.ActionWithID(page, action, "id=" + id);
    }
   
}
